exports.title = "微信抢红包";
exports.description = "收到微信红包通知会自动抢\n1.最好在聊天界面,因为群聊消息太多会没有通知\n2.如果多个群聊请在会话列表页面";
exports.package = ["com.tencent.mm", "android"];
exports.params = [];

exports.script = function(shell) {
    return {
        TYPE_WINDOW_STATE_CHANGED(data) {
            if (data.className == "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI") {
                console.log("开红包");
                var btn;
                var tries = 5;
                while (tries--) {
                    shell.sleep(200);
                    btn = shell.find(function(x) { return x.name == "android.widget.Button"; });
                    if (btn) {
                        break;
                    }
                }
                if (btn) {
                    console.log("开红包", shell.click(btn.path));
                    shell.sleep(1000);
                } else {
                    console.log("红包已领取");
                }
                if (!this.isChating() && this.flag == "openlucky") {
                    shell.pressBack();
                }
                this.flag = "success";
            } else if (this.flag == "opening") {
                if (data.className == "com.tencent.mm.ui.LauncherUI") {
                    console.log("进入微信");
                    // 点击微信
                    // 进入第一个聊天界面
                    var chatlist = this.getChatlist(true);
                    if (chatlist) {
                        console.log("当前在聊天界面");
                        var unread = false;
                        for (let i = 4; i < chatlist.children.length; i++) {
                            var row = chatlist.children[i];
                            if (row.name == "android.widget.LinearLayout") {
                                if (row.children) {
                                    unread = row;
                                    break;
                                }
                            }
                        }
                        this.flag = "findlucky";
                        if (unread) {
                            console.log("进入未读聊天", shell.click(unread.path));
                            this.findlucky(true);
                        } else {
                            console.log("进入第一个聊天列表");
                            for (let i = 4; i < chatlist.children.length; i++) {
                                var row = chatlist.children[i];
                                if (row.id == "com.tencent.mm:id/app" && row.name == "android.widget.LinearLayout") {
                                    shell.click(row.path);
                                    this.findlucky(true);
                                    break;
                                    // if (this.flag != "findlucky") {
                                    //     break;
                                    // }
                                    // shell.sleep(300);
                                }
                            }
                        }
                    }
                } else if (data.className == "com.android.internal.app.ResolverActivity") {
                    console.log("点击系统");
                    shell.sleep(500);
                    // console.log(shell.getView());
                    shell.click(">1") ? console.log("点击系统成功") : console.log("点击系统失败");
                }
            } else if (this.flag == "findlucky") {
                if (data.className == "com.tencent.mm.ui.LauncherUI") { // 查看红包
                    this.findlucky(true);
                }
            }
        },
        TYPE_WINDOW_CONTENT_CHANGED(data) {
            // 在微信内
            if (data.className == "android.widget.ListView" && data.packageName == "com.tencent.mm") {
                var chatlist;
                if (this.isChating()) {
                    this.findlucky();
                } else if (chatlist = this.getChatlist()) {
                    if (chatlist) {
                        console.log("当前在聊天界面");
                        var unread = false;
                        for (let i = 0; i < chatlist.children.length; i++) {
                            var row = chatlist.children[i];
                            if (row.name == "android.widget.LinearLayout") {
                                if (row.children) {
                                    console.log(row);
                                    unread = row;
                                    break;
                                }
                            }
                        }
                        if (unread) {
                            console.log("进入未读聊天", shell.click(unread.path));
                            this.findlucky(true);
                        } else {
                            console.log("没有未读聊天");
                        }
                    }
                } else {
                    console.log("未知界面", data);
                }
            }
        },
        TYPE_NOTIFICATION_STATE_CHANGED(data) {
            // 收到微信红包
            if (data.packageName == "com.tencent.mm" && /\[微信红包]/.test(data.text)) {
                console.log("收到红包");
                this.notify = true;
                this.flag = "findlucky";
                shell.openNotify(data.nid) || this.run();
            } else {
                console.log("不是红包", data);
            }
        },
        isChating() {
            return shell.find(function(x) { return x.description == "聊天信息"; });
        },
        getChatlist(log) {
            var pager = shell.find(function(x) { return x.name == "com.tencent.mm.ui.mogic.WxViewPager"; });
            if (log) {
                if (pager) {
                    console.log("在首页");
                    shell.click(">0>1");
                }
            }
            return shell.find(pager, function(x) { return x.name == "android.widget.ListView"; });
        },
        findlucky(back) {
            console.log("查找红包");
            var tree = shell.getView();
            var div = tree.children[0].children[0].children[0];
            if (div && div.children && div.children.length > 2 && div.children[2].description == "聊天信息") {
                var list = div.children[3].children.reverse();
                var lucky = shell.find(list, function(x) {
                    if (x.name == "android.widget.LinearLayout") {
                        var ok = false;
                        for (let i = 0; x.children && i < x.children.length; i++) {
                            var item = x.children[i];
                            if (/红包已领取/.test(item.text)) {
                                ok = false;
                                break;
                            }
                            if (/查看红包/.test(item.text)) {
                                ok = false;
                                break;
                            }
                            if (/红包已被领完/.test(item.text)) {
                                ok = false;
                                break;
                            }
                            if (/微信红包/.test(item.text)) {
                                ok = true;
                            }
                        }
                        return ok;
                    }
                });
                if (lucky) {
                    console.log("找到红包");
                    shell.click(lucky.path);
                    this.flag = "openlucky";
                } else {
                    console.log("没有找到红包");
                    if (back) {
                        console.log("返回聊天列表");
                        shell.sleep(300);
                        shell.pressBack();
                    }
                }
            } else {
                console.log("不在聊天页面");
            }
        },
        run: function() {
            this.flag = "opening";
            shell.open("com.tencent.mm");
        }
    };
};